const App = {
  mixins: [Query], //配置文件，路径/utils/mixins.js
  data() {
    return {
      monthColors: [
        "#e85858",
        "#f09ab2",
        "#9fbf6a",
        "#6fbbc8",
        "#a79ecb",
        "#f7ac69",
        "#ea5859",
        "#f19ab2",
        "#9fbf6a",
        "#6fbbc7",
        "#a79ecb",
        "#f7ac69",
      ],
      z: [],
      a: "year",
      year: new Date().getFullYear(),
      Month: new Date().getMonth() + 1,
      page: {
        value1: "",
        PageNumber: 1, //页码
        RowAmount: 10,
        Condition: window.btoa(unescape(encodeURIComponent("%%,%%"))),
      },
      statistics: {
        huanjie: {
          total: 0,
          completed: 0,
        },
        buxuan: {
          completed: 0,
        },
      },
    };
  },
  mounted() {
    // this.Treedata();
    this.getData();
    this.yeart();
  },
  computed: {
    isDate() {
      return function (row) {
        if (row.End_Date) {
          if (+new Date(row.End_Date) - +new Date() < 90 * 24 * 3600 * 1000) {
            return "提示换届";
          }
        }
        return "";
      };
    },
  },
  methods: {
    handleCB(row) {
      this.$confirm('该操作将'+ (row.Preparation == '0'?'':'取消') +'标记为筹备中,是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        $.ajax({
          headers: requestHeaders(),
          url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
          method: "post",
          data: {
            Data: 'village_organization_tenure',
            Condition: "Tenure_SerialNumber='" + row.Tenure_SerialNumber + "'"
          },
          success: (res) => {
            if (res.length == 1) {
              console.log(res[0]);
              res[0].Preparation = row.Preparation == '0'? '1':'0';
              let lJSONObject_DataSet = {
                Modify: {
                  village_organization_tenure: res
                }
              }
              let Operation_Main = JSON.stringify(deepClone(lJSONObject_DataSet));
              lJSONObject_DataSet['Add'] = new Object();
              lJSONObject_DataSet['Add']['base_operation_log'] = [{
                Operation_Type: '0',
                Operation_Path: this.relativePath,
                Operation_Main,
                Operation_Interface: "generalSaveByRequestBody",
                Operation_IP: getIpAddress()
              }]
              submitSave(lJSONObject_DataSet, (res) => {
                if (res.Status == 'success') {
                  this.postgetList(this.page);
                } else {
                  this.$message({
                    type: 'error',
                    message: '删除失败'
                  });
                }
              })
            } else {
              this.$message({
                type: 'error',
                message: '操作失败'
              });
            }
          },
          complete: (XMLHttpRequest) => {
            requestPretreatment(XMLHttpRequest)
          },
        });
      }).catch(() => { });
    },
    mout() {
      this.a = "mout";
      console.log(this.a);
      $.ajax({headers: requestHeaders(),
        url: `${mh}/partyOrgChange/queryChangeAmountAndCompleteAmountByMonth`,
        method: "get",
        data: {},
        success: (data) => {
          console.log(data);
          // 直接转换后端返回的数据格式
          const monthlyData = data.map((item) => ({
            month: parseInt(item.Month), // 将字符串月份转换为数字
            opened: item.Amount, // 重命名字段
            total: item.Complete_Amount, // 重命名字段
          }));

          console.log(monthlyData);
          // return;
          monthlyData.forEach((item) => {
            console.log(item);
            const month = Number(item.Month);
            if (month >= 1 && month <= 12) {
              const index = month - 1;
              monthlyData[index].opened = Number(item.opened) || 0;
              monthlyData[index].total = Number(item.total) || 0;
            }
          });
          this.statistics.huanjie = {
            months: monthlyData,
          };

          console.log("按月数据：", monthlyData);
          this.z = monthlyData;
          console.log(this.z);
        },
        complete: (XMLHttpRequest) => {

          requestPretreatment(XMLHttpRequest);
        },
      });
    },
    yeart() {
      this.a = "year";
      $.ajax({headers: requestHeaders(),
        url: `${mh}/partyOrgChange/queryChangeAmountAndCompleteAmount`,
        method: "get",
        data: {},
        success: (data) => {
          console.log(data);
          data.forEach((item) => {
            console.log(item);
            this.statistics.huanjie.total = item.Amount;
            this.statistics.huanjie.completed = item.Complete_Amount;
          });
        },
        complete: (XMLHttpRequest) => {

          requestPretreatment(XMLHttpRequest);
        },
      });
    },
    handleEdit(row) {
      let { primary_key } = JSON.parse(
        document.getElementById("table").getAttribute("data-options")
      );
      console.log(row[primary_key]);
      if (row[primary_key]) {
        window.location.href =
          "./add.html?action=Modify&nodeKey=" + row[primary_key];
      } else {
        window.location.href =
          "./add.html?action=Add&nodeKey=" +
          row["Party_Organization_SerialNumber"];
      }
    },
    bx() {
      const _this = this;
      $.ajax({headers: requestHeaders(),
        url: `${mh}/partyOrgChange/queryByElectionPartyOrgAmount`,
        method: "get",
        data: {},
        success: (data) => {
          console.log(data);
          data.forEach((item) => {
            console.log(item);
            _this.statistics.buxuan.completed = item.By_Election_Amount;
          });
        },
        complete: (XMLHttpRequest) => {

          requestPretreatment(XMLHttpRequest);
        },
      });
    },
    getData() {
      const _this = this;
      $.ajax({headers: requestHeaders(),
        // url: `${mh}/generalBusiness/generalQueryBySQLSerialNumber`,
        url: `${mh}/partyOrgChange/queryChangeAmountAndCompleteAmount`,
        method: "get",
        data: {
          // SQLSerialNumber: 'f_tenure_count',
          // Condition: '',
          // ExtensionCondition: '',
        },
        success: (data) => {
          console.log(data);
          data.forEach((item) => {
            console.log(item);

            _this.bx();
            // switch (item.type) {
            // case "换届":
            // this.statistics.huanjie.total = item.Amount;
            // this.statistics.huanjie.completed = item.Complete_Amount;
            // break;
            // case "补选":
            // this.statistics.buxuan.total = item.Complete_Amount;
            // this.statistics.buxuan.completed = item.Amount;
            // break;
            // }
          });
        },
        complete: (XMLHttpRequest) => {

          requestPretreatment(XMLHttpRequest);
        },
      });
    },
    //
    //换届补选按钮，跳转到组织架构页面
    structureAdd(row) {
      window.location.href =
        "structure.html?nodeKey=" +
        row.Tenure_SerialNumber +
        "&VillageNumber=" +
        row.VillageNumber;
    },
    //换届登记按钮，跳转到添加换届领导班子页面
    leaderAdd() {
      window.location.href = "leaderAdd.html";
    },
    //换届编辑按钮，跳转到修改换届页面
    tenureEdit() {
      window.location.href = "tenureEdit.html";
    },
    //树的数据
    Treedata() {
      $.ajax({headers: requestHeaders(),
        url: `${mh}/clientOperation/getDistrictTree`,
        type: "GET",
        data: {
          DistrictCode: "232723",
        },
        success: (data) => {
          this.treeData = data; // 直接使用 this
        },
        complete: (XMLHttpRequest) => {

          requestPretreatment(XMLHttpRequest);
        },
        dataType: "json",
      });
    },
    check() {
      this.page.Condition = window.btoa(
        unescape(encodeURIComponent("%%,%" + this.page.value1 + "%"))
      );
      this.postgetList(this.page);
    },
    //树的点击事件
    handleNodeClick(data) {
      this.page.Condition = window.btoa(
        unescape(
          encodeURIComponent(
            data.ProvinceNumber + "%,%" + this.page.value1 + "%"
          )
        )
      );
      this.postgetList(this.page);
    },
  },
};

const { createApp } = Vue;
const app = createApp(App);
//这种方式是引入全部图标，无需单个引入
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component);
}
const ElMessage = ElementPlus.ElMessage;
app.use(ElementPlus, { locale: zhCn });
app.mount("#app");
